<?php

	$errmsg=GetMessageStack();
	if(isset($_GET['redir']))
		$redirect=trim($_GET['redir']);
	else 
		$redirect='';
			
	if(isset($_POST['friend_email']))
		$email=trim($_POST['friend_email']);
	else 
		$email='';
			
	if(isset($_POST['vcode']))	
		$vcode = trim($_POST['vcode']);
	else 
		$vcode='';
		
	$logid=GetLoginUserID();
	$userDao=GetUserDAO();
	
	
	function InviteFriend()
	{
		global $userDao, $logid, $errmsg, $email, $vcode;
		
		/*
		 * 服务器端验证
		 */
		if( $vcode != edp_getsession( 'vcode' ) )
		{
			$errmsg->add( 'friend_invite', '验证码无效' );
			return;
		}
		
		if( edp_is_empty_string($email) )
		{
			$errmsg->add( 'friend_invite', '请输入email地址' );
			return;
		}
		
		$emailArr = explode(',',$email);
		

		if(count($emailArr)>10||count($emailArr)<1){
			
			$errmsg->add( 'friend_invite', '输入1到10个email地址' );
			return;			
		}
		
		$emailArr = array_unique($emailArr);
		
		foreach($emailArr as $k=>$v){
			
			if( !preg_match('/^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$/', $v) )
			{
				$errmsg->add( 'friend_invite', 'Email地址不合法！' );
				return;
			}					
		}

			
		/**
		 * remove by bo:这个无所谓，可以再用其他邮箱注册
		 */
//		$register_id=$userDao->GetUserByEmail( $email );
//		if( $register_id!=false )
//		{
//			$errmsg->add( 'friend_invite', '您的好友已经注册！<a href="'.edp_href_link(PAGENAME_USER_INFO, 'uid='.$register_id).'">点击</a>跳转至主页！' );
//			return;
//		}
//		

		$email = implode(' , ',$emailArr);
		
		$inviter=$userDao->GetUserByID($logid);
		
		//Send Mail
		$mail=GetMailer();
		$mail->SendInviteMail($emailArr, $inviter['email'], $inviter['id']);
		
		TransferTo('invite_friend_success');
	}
	
	if($logid==false)
	{
		TransferTo('unlogin');
	}
	else
	{
		if(isset($_POST['action']))
			if(trim($_POST['action'])=='invite_friend')
				InviteFriend();
	}
?>